FROM debian:11.2

COPY dockerfiles/php/sources.list /etc/apt/sources.list
RUN cat /etc/apt/sources.list
RUN apt-get clean && apt-get update
RUN apt-get install wget -y

RUN apt-get install gcc automake autoconf libtool make wget build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev libxml2-dev libcurl4-openssl-dev libjpeg-dev libpng-dev libpq-dev libicu-dev libfreetype6-dev libldap2-dev libxslt-dev libmariadb-dev-compat libmariadb-dev libgmp-dev libgmp3-dev libxpm-dev pkg-config libfreetype6-dev libjpeg62-turbo-dev libpng-dev patch -y

#libfreetype6-dev


ENV LIBMCRYPT_VERSION 2.5.8
ENV MHASH_VERSION 0.9.9.9
ENV ONIGURUMA_VERSION 6.9.4
ENV PHP_VERSION 7.2.24
ENV SWOOLE_VERSION 4.4.14
ENV AMQP_VERSION 1.9.4
ENV RABBITMQ_C_VERSION 0.10.0
ENV REDIS_VERSION 5.1.1
ENV PATH=$PATH:/usr/local/php/bin/:/usr/local/php/sbin/

RUN groupadd -r php \
    && useradd -r -g php -s /sbin/nologin -d /usr/local/php -M php


COPY dockerfiles/php/libmcrypt-2.5.8.tar.gz libmcrypt.tar.gz 
# RUN wget -c https://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/${LIBMCRYPT_VERSION}/libmcrypt-${LIBMCRYPT_VERSION}.tar.gz -O libmcrypt.tar.gz \ 
RUN mkdir -p libmcrypt \
    && tar -xf libmcrypt.tar.gz -C libmcrypt --strip-components=1 \
    && rm -f libmcrypt.tar.gz \
    && ( \
    cd libmcrypt \
    && ./configure \
    && make -j8 \
    && make install \
    ) \
    && rm -rf libmcrypt

COPY dockerfiles/php/mhash-0.9.9.9.tar.gz mhash.tar.gz 
RUN mkdir -p mhash \
    && tar -xf mhash.tar.gz -C mhash --strip-components=1 \
    && rm -f mhash.tar.gz \
    && ( \
    cd mhash \
    && ./configure \
    && make -j8 \
    && make install \
    ) \
    && rm -rf mhash

#COPY dockerfiles/php/freetype-2.10.2.tar.gz freetype.tar.gz
##RUN mkdir -p freetype \
#RUN mkdir -p freetype \
#    && tar -xf freetype.tar.gz -C freetype --strip-components=1 \
#    && rm -f freetype.tar.gz \
#    && ( \
#    cd freetype \
#    && ./configure --prefix=/usr/local/freetype \
#    && make -j10 \
#    && make install \
#    ) \
#    && rm -rf freetype


COPY dockerfiles/php/oniguruma-6.9.4.tar.gz oniguruma.tar.gz 
# RUN wget -c https://github.com/kkos/oniguruma/archive/v${ONIGURUMA_VERSION}.tar.gz -O oniguruma.tar.gz \
RUN mkdir -p oniguruma \
    && tar -xf oniguruma.tar.gz -C oniguruma --strip-components=1 \
    && rm -f oniguruma.tar.gz \
    && ( \
    cd oniguruma \
    && ./autogen.sh \
    && ./configure --prefix=/usr \
    && make -j8 \
    && make install \
    ) \
    && rm -rf oniguruma
    # && wget -c https://www.php.net/distributions/php-${PHP_VERSION}.tar.gz --no-check-certificate -O php.tar.gz \

COPY dockerfiles/php/openssl-1.0.2k.tar.gz openssl-1.0.2k.tar.gz
RUN mkdir -p openssl \
    && tar -xf openssl-1.0.2k.tar.gz -C openssl --strip-components=1 \
    && rm -f openssl-1.0.2k.tar.gz \
    && ( \
    cd openssl \
    && ./config \
    && make -j8 \
    && make install \
    ) \
    && rm -rf openssl

COPY dockerfiles/php/php-7.2.24.tar.gz php.tar.gz
RUN mkdir -p /usr/local/php/etc \
    && mkdir -p php \
    && tar -xf php.tar.gz -C php --strip-components=1 \
    && rm -f php.tar.gz \
    && ( \
    cd php \
    && sed -i "s/freetype-config/pkg-config/g" ./ext/gd/config.m4 \
    && sed -i "s/FREETYPE2_CONFIG --cflags/FREETYPE2_CONFIG freetype2 --cflags/g" ./ext/gd/config.m4 \
    && sed -i "s/FREETYPE2_CONFIG --libs/FREETYPE2_CONFIG freetype2 --libs/g" ./ext/gd/config.m4 \
    && ./buildconf --force \
    && ./configure \
        --prefix=/usr/local/php \
        --exec-prefix=/usr/local/php \
        --bindir=/usr/local/php/bin \
        --sbindir=/usr/local/php/sbin \
        --includedir=/usr/local/php/include \
        --libdir=/usr/local/php/lib/php \
        --mandir=/usr/local/php/php/man \
        --with-config-file-path=/usr/local/php/etc \
        --with-mysql-sock=/var/run/mysql/mysql.sock \
        --with-fpm-user=php \
        --with-fpm-group=php \
        --with-openssl \
        --with-mhash \
        --with-sqlite3 \
        --with-zlib \
        --with-iconv \
        --with-bz2 \
        --with-curl \
        --with-cdb \
        --with-openssl-dir \
        --with-zlib-dir \
        --with-jpeg-dir=/usr/include/ \
        --with-freetype-dir=/usr/include/ \
        --with-gettext \
        --with-gmp \
        --with-mhash \
        --with-mysqli=mysqlnd \
        --with-pdo-mysql=mysqlnd \
        --with-zlib-dir \
        --with-pgsql \
        --with-pdo-pgsql \
        --with-readline \
        --with-xsl \
        --with-pear \
        --with-gd \
        --enable-gd \
        --enable-fpm \
        --enable-inline-optimization \
        --enable-shared \
        --enable-soap \
        --enable-pcntl \
        --enable-xml \
        --enable-xmlreader \
        --enable-xmlwriter \
        --enable-bcmath \
        --enable-calendar \
        --enable-dom \
        --enable-exif \
        --enable-fileinfo \
        --enable-filter \
        --enable-ftp \
        --enable-gd-jis-conv \
        --enable-json \
        --enable-mbstring \
        --enable-mbregex \
        --enable-pdo \
        --enable-session \
        --enable-shmop \
        --enable-simplexml \
        --enable-sockets \
        --enable-sysvmsg \
        --enable-sysvsem \
        --enable-sysvshm \
        --enable-mysqlnd-compression-support \
        --enable-opcache \
        --enable-zip \
        --disable-debug \
        --disable-rpath \
    && make -j8 \
    && make install \
    )

RUN cp php/php.ini-production /usr/local/php/etc/php.ini \
    && echo "extension_dir=\"/usr/local/php/lib/php/extensions/no-debug-non-zts-20170718\"" >> /usr/local/php/etc/php.ini \
    && cp php/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm \
    && cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf \
    && cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf \
#    && echo "export PATH=$PATH:/usr/local/php/bin/:/usr/local/php/sbin/" > /etc/profile.d/php.sh \
#    && . /etc/profile.d/php.sh \
    && rm -rf php


COPY dockerfiles/php/swoole-src-4.4.14.tar.gz swoole.tar.gz 
# RUN wget -c https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O swoole.tar.gz \
RUN mkdir -p swoole \
    && tar -zxvf swoole.tar.gz -C swoole --strip-components=1 \
    && rm -f swoole.tar.gz \
    && ( \
    cd swoole \
    && /usr/local/php/bin/phpize \
    && ./configure --with-php-config=/usr/local/php/bin/php-config --enable-openssl \
    && make -j8 \
    && make install \
    ) \
    && rm -rf swoole \
    && sed -i "2i extension=swoole.so" /usr/local/php/etc/php.ini

# RUN wget -c https://github.com/alanxz/rabbitmq-c/archive/v${RABBITMQ_C_VERSION}.tar.gz -O rabbitmq.tar.gz \
#     && mkdir -p rabbitmq \
#     && tar -xf rabbitmq.tar.gz -C rabbitmq --strip-components=1 \
#     && rm -f rabbitmq.tar.gz \
#     && ( \
#     cd rabbitmq \
#     && mkdir build \
#     && cd build \
#     && cmake -DCMAKE_INSTALL_PREFIX=/usr/local/rabbitmq .. \
#     && cmake --build .  --target install \
#     && cp -r /usr/local/rabbitmq/lib64 /usr/local/rabbitmq/lib \
#     ) \
#     && rm -rf rabbitmq

# RUN wget -c https://pecl.php.net/get/amqp-${AMQP_VERSION}.tgz -O amqp.tgz \
#     && mkdir -p amqp \
#     && tar -xf amqp.tgz -C amqp --strip-components=1 \
#     && rm -f amqp.tgz \
#     && ( \
#     cd amqp \
#     && /usr/local/php/bin/phpize \
#     && ./configure \
#     --with-php-config=/usr/local/php/bin/php-config \
#     --with-amqp \
#     --with-librabbitmq-dir=/usr/local/rabbitmq \
#     && make \
#     && make install \
#     ) \
#     && rm -rf amqp \
#     && sed -i "2i extension=amqp.so" /usr/local/php/etc/php.ini \
#     && ln -s /usr/local/rabbitmq-c/lib64/librabbitmq.so.4 /usr/lib64/librabbitmq.so.4

WORKDIR /app
# RUN wget -c https://github.com/phpredis/phpredis/archive/${REDIS_VERSION}.tar.gz -O phpredis.tar.gz \
#     && mkdir -p phpredis \
COPY dockerfiles/php/phpredis-5.1.1.tar.gz /app/phpredis.tar.gz
RUN mkdir -p phpredis \
    && tar -zxvf phpredis.tar.gz -C phpredis --strip-components=1 \
    && rm -f phpredis.tar.gz \
    && ( \
    cd phpredis \
    && /usr/local/php/bin/phpize \
    && ./configure \
    --with-php-config=/usr/local/php/bin/php-config \
    --enable-redis \
    && make -j8 \
    && make install \
    ) \
    && rm -rf phpreids \
    && sed -i "2i extension=redis.so" /usr/local/php/etc/php.ini


COPY dockerfiles/php/swoole-loader/swoole_loader72.so /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/swoole_loader72.so
RUN sed -i "2i extension=swoole_loader72.so" /usr/local/php/etc/php.ini


COPY dockerfiles/php/sdebug-sdebug_2_9-beta.tar.gz /app/sdebug.tar.gz
RUN mkdir -p sdebug \
    && tar -zxvf sdebug.tar.gz -C sdebug --strip-components=1 \
    && rm -f sdebug.tar.gz \
    && ( \
    cd sdebug \
    && /usr/local/php/bin/phpize \
    && ./configure \
    --with-php-config=/usr/local/php/bin/php-config \
    --enable-xdebug \
    && make -j8 \
    && make install \
    ) \
    && rm -rf sdebug \
    && sed -i "2i zend_extension=xdebug.so" /usr/local/php/etc/php.ini

RUN sed -i "2i xdebug.profiler_enable=0" /usr/local/php/etc/php.ini \
    && sed -i "2i xdebug.profiler_enable_trigger=1" /usr/local/php/etc/php.ini \
    && sed -i "2i xdebug.profiler_output_dir=/app/xdebug" /usr/local/php/etc/php.ini \
    && sed -i "2i xdebug.profiler_append=0" /usr/local/php/etc/php.ini

# RUN wget -c https://pecl.php.net/get/mongodb-1.7.1.tgz -O mongodb.tgz \
#     && mkdir -p mongodb \
#     && tar -xf mongodb.tgz -C mongodb --strip-components=1 \
#     && rm -f mongodb.tgz \
#     && ( \
#     cd mongodb \
#     && /usr/local/php/bin/phpize \
#     && ./configure \
#     --with-php-config=/usr/local/php/bin/php-config \
#     && make \
#     && make install \
#     ) \
#     && rm -rf mongodb \
#     && sed -i "2i extension=mongodb.so" /usr/local/php/etc/php.ini

RUN /usr/local/php/bin/php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
    && /usr/local/php/bin/php composer-setup.php \
    && chmod a+x composer-setup.php \
    && rm -f composer-setup.php

RUN mv composer.phar /usr/local/bin/composer \
    && chmod a+x /usr/local/bin/composer \
    && /usr/local/php/bin/php /usr/local/bin/composer config -g repo.packagist /usr/local/bin/composer https://mirrors.aliyun.com/composer/
# RUN apt install php-gd \
#     && cp -f /usr/lib64/php/modules/gd.so /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/gd.so \
#     && sed -i "2i extension=gd.so" /usr/local/php/etc/php.ini
CMD ["/usr/local/php/sbin/php-fpm", "-R", "--nodaemonize"]
WORKDIR /app

EXPOSE 9000
